<?php

/**
 * article actions.
 *
 * @package    article
 * @subpackage article
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class articleActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      	
  	$this->article_featured_list = Doctrine::getTable('ArticleArticle')->retrieveFeaturedArticles(sfConfig::get('app_max_featured_article'));
  	$this->article_latest_list = Doctrine::getTable('ArticleArticle')->retrieveLatestArticles(sfConfig::get('app_max_latest_article'));
  	
  }

  public function executeShow(sfWebRequest $request)
  {
    //$this->article_article = Doctrine::getTable('ArticleArticle')->find($request->getParameter('id'));
    
    //$this->job = Doctrine::getTable('JobeetJob')->retrieveActiveArticle(Doctrine_Query::create()->from('ArticleArticle a')->where('a.id = '.$request->getParameter('id')));
    $this->article_article = $this->getRoute()->getObject();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ArticleArticleForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ArticleArticleForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($article_article = Doctrine::getTable('ArticleArticle')->find($request->getParameter('id')), sprintf('Object article_article does not exist (%s).', $request->getParameter('id')));
    $this->form = new ArticleArticleForm($article_article);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($article_article = Doctrine::getTable('ArticleArticle')->find($request->getParameter('id')), sprintf('Object article_article does not exist (%s).', $request->getParameter('id')));
    $this->form = new ArticleArticleForm($article_article);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($article_article = Doctrine::getTable('ArticleArticle')->find($request->getParameter('id')), sprintf('Object article_article does not exist (%s).', $request->getParameter('id')));
    $article_article->delete();

    $this->redirect('article/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $article_article = $form->save();

      $this->redirect('article/edit?id='.$article_article->getId());
    }
  }
}
